package com.jxtc.enterprise.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxtc.enterprise.admin.service.AdminFoodManagementService;
import com.jxtc.enterprise.admin.vo.AdminFoodVO;
import com.jxtc.enterprise.admin.vo.StoreWithFoodTypeVO;
import com.jxtc.enterprise.common.aspectj.TokenRequired;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.common.vo.PaginationResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jxtc/enterprise/admin/foodManage")
@RequiredArgsConstructor
@Slf4j
public class AdminFoodManagementController {

    private final AdminFoodManagementService adminFoodManagementService;


    @ApiResponse(responseCode = "200", description = "code 为 200 时，data 字段为包含菜品列表的分页信息")
    @Parameter(name = "size", description = "每页显示的记录数，默认为 10")
    @Parameter(name = "page", description = "当前页码，从 1（默认值）开始")
    @Parameter(name = "tenantName", description = "租户名称关键字")
    @Parameter(name = "storeId", description = "店铺 ID")
    @Parameter(name = "keyword", description = "查询关键字，根据菜品名称模糊查询")
    @Operation(summary = "1.5.1 管理员分页查询菜品信息列表", tags = {"1.5 管理员菜品管理"}, operationId = "1.5.1")
    @TokenRequired(role = TokenRequired.ROLE_ADMIN)
    @GetMapping("/pageFoodByNameOrStoreId")
    public Result<PaginationResponseVO<AdminFoodVO>> pageFoodByNameOrStoreIdForAdmin(@RequestParam(required = false) String keyword,
                                                                                     @RequestParam(required = false) String storeId,
                                                                                     @RequestParam(value = "tenantName", required = false) String tenantName,
                                                                                     @RequestParam(required = false, defaultValue = "1") int page,
                                                                                     @RequestParam(required = false, defaultValue = "10") int size) {
        log.info("/pageFoodByNameOrStoreId, keyword: {}, storeId: {}, page: {}, size: {}", keyword, storeId, page, size);
        PaginationResponseVO<AdminFoodVO> vo = adminFoodManagementService.pageFoodByNameOrStoreIdForAdmin(keyword, storeId, tenantName, page, size);
        return ResultGenerator.genSuccessResult(vo);
    }


    @ApiResponse(responseCode = "200", description = "code 为 200 时，data 字段为包含菜品信息")
    @Parameter(name = "foodId", description = "菜品 ID")
    @Operation(summary = "1.5.2 管理员根据 ID 查询菜品信息", tags = {"1.5 管理员菜品管理"}, operationId = "1.5.2")
    @TokenRequired(role = TokenRequired.ROLE_ADMIN)
    @GetMapping("/queryFoodById")
    public Result<AdminFoodVO> queryFoodByIdForAdmin(@RequestParam(value = "foodId") String foodId) {
        log.info("/queryFoodById, foodId: {}", foodId);
        AdminFoodVO vo = adminFoodManagementService.queryFoodByIdForAdmin(foodId);

        log.info("/queryFoodById, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }


    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示软删除菜品成功，此时 data 字段为空")
    @ApiResponse(responseCode = "200", description = "code 为 400 时， 表示软删除品失败，此时 message 字段为错误提示信息")
    @Parameter(name = "foodId", description = "菜品 ID")
    @Operation(summary = "1.5.3 管理员根据 ID 删除指定菜品", tags = {"1.5 管理员菜品管理"}, operationId = "1.5.3")
    @TokenRequired(role = TokenRequired.ROLE_ADMIN)
    @DeleteMapping("/deleteFoodById")
    public Result<Void> deleteFoodByIdForAdmin(@RequestParam(value = "foodId") String foodId) {
        log.info("/deleteFoodById, foodId: {}", foodId);
        adminFoodManagementService.deleteFoodById(foodId);

        log.info("/deleteFoodById, delete food: {} successfully", foodId);
        return ResultGenerator.genSuccessResult();
    }


    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示获取店铺 ID 及菜品分类成功，此时 data 字段为信息列表")
    @Operation(summary = "1.5.4 管理员获取店铺 ID 及菜品分类接口", tags = {"1.5 管理员菜品管理"}, operationId = "1.5.4")
    @TokenRequired(role = TokenRequired.ROLE_ADMIN)
    @GetMapping("/listStoreAndFoodTypes")
    public Result<List<StoreWithFoodTypeVO>> listStoreAndFoodTypesForAdmin() {
        List<StoreWithFoodTypeVO> voList = adminFoodManagementService.listStoreAndFoodTypesForAdmin();

        log.info("/listStoreAndFoodTypes, response: {}", JSONObject.toJSONString(voList));
        return ResultGenerator.genSuccessResult(voList);
    }
}
